
from traceback import print_exc
from visual import *
from PythonCard import dialog

class ExecutedItem(Exception):
	def __init__(self):
		Exception.__init__(self, 'ExecutedItem')

class GenericMenu:
	def __init__(self, fields={}):
		""" pass dict mapping name to meny/callable
		"""
		self.fields = fields
		self.back = None
		## create labels:
		self._display = label(xoffset=40, yoffset=-15, height=14, border=10, line=0)
		text, index = '', 1
		for description in fields.keys():
			text += '%d : %s\n' % (index, description,)
			index += 1
		self._display.text = text
		self._display.visible = 0
	
	def hide(self):
		self._display.visible = 0
	def show(self, pos):
		self._display.pos = pos
		self._display.visible = 1
	def move(self, pos):
		self._display.pos = pos
	
	def on_key_event(self, key):
		try:
			key = int(key) - 1
			if key >= 0 and key < len(self.fields):
				return self.fields[ self.fields.keys()[key] ]
			else:
				return None
		except:
			return None

class ActionMenu:
	def __init__(self, dialog_parent):
		self._dialog_parent = dialog_parent
		site_mapping = {
			'(1) terrain level site' : lambda: self.insert_approachsite_step(1),
			'(2) raised middle site' : lambda: self.insert_approachsite_step(2),
			'(3) cylindrical site' : lambda: self.insert_approachsite_step(3),
			}
		main_mapping = {
			'build' : self.insert_build_step,
			'place lintel' : self.insert_placelintel_step,
			'aproach to site' : GenericMenu(site_mapping),
			'find random column pattern' : self.insert_findpattern_step,
			'chase column element' : self.insert_chasepak_step,
			}
		self._root = GenericMenu(main_mapping)
		self._shown = False
		self._submenu = None
		self._pos = (0, 0)
	
	def on_mousemove_event(self, pos):
		self._pos = pos
		if self._submenu:
			self._submenu.move(pos)
	
	def on_key_event(self, key, plan):
		if self._submenu:
			res = self._submenu.on_key_event(key)
			if isinstance(res, GenericMenu):
				self._submenu.hide()
				self._submenu = res
				res.show(self._pos)
			elif callable(res):
				self.plan = plan
				
				try:
					res()
				except:
					print_exc()

				self.plan = None
				self._submenu.hide()
				self._submenu = None
				raise ExecutedItem()
			else: return False
			return True
		return False
	
	def show(self):
		""" show root menu """
		self._shown = True
		if self._submenu: self._submenu.hide()
		self._submenu = self._root
		self._submenu.show(self._pos)
	def hide(self):
		""" hide """
		self._shown = False
		if self._submenu: self._submenu.hide()
		self._submenu = None
	
	def enter_time_constraint(self):
		if self.plan == None: raise Exception('NO self.plan !')
		result = dialog.textEntryDialog(self._dialog_parent, 'max duration:', 'time constraint', '15')
		if not result.accepted: raise Exception('NO self.plan !')
		return float(result.text)

	def insert_build_step(self):
		self.plan.add_build_step(self.enter_time_constraint())
		print 'build_step added ..'
	
	def insert_placelintel_step(self):
		self.plan.add_placelintel_step(self.enter_time_constraint())
		print 'placelintel added ..'

	def insert_approachsite_step(self, site):
		self.plan.add_approachsite_step(site, self.enter_time_constraint())
		print 'build_step added ..', site

	def insert_findpattern_step(self):
		print 'about to add find pattern step ..'
		self.plan.add_findpattern_step(self.enter_time_constraint())
		print 'find pattern step added ..'
	
	def insert_chasepak_step(self):
		self.plan.add_chasepak_step(self.enter_time_constraint())
		print 'chase step added ..'








